from models.meta import BaseDocument, IdField, fields, BaseEmbeddedDocument


class CustomProfileTemplate(BaseDocument):
    meta = {
        'db_alias': 'kb',
        'collection': "custom_profile_template",
        'primary_key': 'custom_profile_template_id',
        'indexes': ['custom_profile_template_id', '-created_at', 'name', 'description'],
        'ordering': ['-created_at']
    }

    custom_profile_template_id = IdField(unique=True)
    name = fields.StringField(unique=True, max_length=20, default='')
    description = fields.StringField(max_length=100, default='')
    plugin_list = fields.ListField(default=[])
    custom_id = IdField()
    display_name = fields.DictField(unique=True, default={})
    is_disabled = fields.BooleanField(default=False)


class SystemProfileTemplate(BaseDocument):
    meta = {
        'db_alias': 'kb',
        'collection': "system_profile_template",
        'primary_key': '_id',
        'ordering': ['-created_at']
    }

    value = IdField(unique=True)
    name = fields.StringField(default='')
    description = fields.StringField(default='')
    plugin_list = fields.ListField(default=[])
    display_name = fields.DictField(default={})
    is_disabled = fields.BooleanField(default=False)


class ScanPageBaselineEmbeddedDocument(BaseEmbeddedDocument):
    url = fields.StringField()
    type = fields.IntField(choices=(1, 2, 3))  # 1 表示网页文本, 2 表示网页中的图片资源，3 表示jss、css等其他资源文件
    md5 = fields.StringField()
    size = fields.IntField()
    content_type = fields.StringField()
    content = fields.StringField()
    is_error = fields.BooleanField()    # waf、识别不了资源类型等
    is_existing = fields.BooleanField()     # 是否存在响应或者状态码为404


class ScanPageBaseline(BaseDocument):
    """页面变更基准数据"""

    task_id = IdField()
    url = fields.StringField()
    text = fields.EmbeddedDocumentField(document_type=ScanPageBaselineEmbeddedDocument)
    structure = fields.EmbeddedDocumentField(document_type=ScanPageBaselineEmbeddedDocument)
    resources = fields.EmbeddedDocumentListField(document_type=ScanPageBaselineEmbeddedDocument)
    created_at = fields.StringField()
    updated_at = fields.StringField()

    meta = {
        'collection': 'scan_page_baselines',
        'primary_key': 'task_id',
        'indexes': ['task_id', ],
    }
